
//
//  NetworkSharedManager.m
//  yiwangMall
//
//  Created by Mac on 16/1/7.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "NetworkSharedManager.h"
#import "AppDelegate.h"
#import "TimeExpansion.h"
@implementation NetworkSharedManager

+ (NetworkSharedManager *)sharedManager{
    
    static NetworkSharedManager *_shareAFHTTPSessionManager = nil;
    static dispatch_once_t onceToken; //多线程技术，一般用来写单例
    
    dispatch_once(&onceToken, ^{
        
        _shareAFHTTPSessionManager = [[self alloc] init];
        
        _shareAFHTTPSessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil];
        //        设置解析格式
        //_shareAFHTTPSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
        //_shareAFNHttpRequestOPManager.requestSerializer.timeoutInterval=60.0;
        //        [_shareAFNHttpRequestOPManager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        //        [_sharedClient.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    });
    return _shareAFHTTPSessionManager;
}

//上传单个图片
+(void)postWithUrl:(NSString *)url andParameters:(NSDictionary *)parameters andImageData:(NSData *)imageData Name:(NSString *)name progressValue:(void (^)(double)) progress_block success:(void (^)(id))success_block failure:(void (^)(id))failure_blcok{
    
    [[[self class] sharedManager] POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
        if (imageData != nil) {
            
            
            int x1 = arc4random() % 100;
            int x2 = arc4random() % 100;
            NSString * filename=[NSString stringWithFormat:@"%d%d%@",x1,x2,[TimeExpansion nowTimeWithFormatter:@"yyyyMMddHHmmss" type:Time_All]];
            [formData appendPartWithFileData:imageData name:name fileName:[NSString stringWithFormat:@"%@.jpg",filename] mimeType:@"image/jpeg"];
        }
    }progress:^(NSProgress * progress){
        progress_block(progress.fractionCompleted);
    }success:^(NSURLSessionDataTask *task, id responseObject){
        success_block(responseObject);
    }failure:^(NSURLSessionDataTask *task, NSError * error){
        failure_blcok(error);
    }];
}

+(void)postWithUrl:(NSString *) url andParameters:(NSDictionary *) parameters progressValue:(void (^)(double)) progress_block success:(void (^)(id)) success_block failure:(void (^)(id)) failure_blcok{
    
    /**
     *  AFNetworkReachabilityStatusUnknown          = -1,  // 未知
     *  AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
     *  AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G
     *  AFNetworkReachabilityStatusReachableViaWiFi = 2,   // 局域网络Wifi
     */
    [[self sharedManager] POST:url parameters:parameters progress:^(NSProgress * progress){
        progress_block(progress.fractionCompleted);
    }success:^(NSURLSessionDataTask *task, id responseObject){
        success_block(responseObject);
    }failure:^(NSURLSessionDataTask *task, NSError * error){
        failure_blcok(error.localizedDescription);
    }];
}
+(void)getWithUrl:(NSString *) url andParameters:(NSDictionary *) parameters  progressValue:(void (^)(double)) progress_block success:(void (^)(id)) success_block failure:(void (^)(id)) failure_blcok{
    
    [[self sharedManager] GET:url parameters:parameters progress:^(NSProgress * progress){
        progress_block(progress.fractionCompleted);
    }success:^(NSURLSessionDataTask *task, id responseObject){
        success_block(responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError * error){
        failure_blcok(error);
    }];
}

#pragma mark 取消网络请求
+ (void)cancelRequest{
    [[[[self class] sharedManager] operationQueue] cancelAllOperations];
}

@end
